<?php

namespace app\user\logic;

use app\model\Abnormal;
use app\model\AbnormalType;
use think\facade\Db;

class AbnormalLogic
{
    protected Abnormal $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Abnormal();
        $this->factory_id = request()->user_info['factory_id'];
    }

    /**
     * @Description 列表
     * @param array $param
     * @time 2024/03/21 16:32:33
     * @return array
     * @author: zj
     */
    public function getList(array $param)
    {
        $sql = "SELECT 
                    a.out_id as id,a.factory_id,f.name as factory,a.barn_id,b1.name as barn,a.fence_id,fe1.name as fence,a.case_id,
                    CASE
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_BARN . " THEN b.name
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_FENCE . " THEN fe.name
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_PIG . " THEN p.tag_address
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_FEED . " THEN feed.name
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_MEDICINE . " THEN medicine.name
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_DEVICE . " THEN m.machine_code
                        ELSE NULL
                    END AS case_name,
                    CASE
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_BARN . " THEN b.status
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_FENCE . " THEN fe.status
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_PIG . " THEN p.health
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_FEED . " THEN feed.status
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_MEDICINE . " THEN medicine.status
                        WHEN a.abnormal_type = " . AbnormalType::TYPE_DEVICE . " THEN m.status
                        ELSE NULL
                    END AS case_status,a.abnormal_type,a.sec_abnormal_type_id,sat.name as sec_abnormal_type,a.error_text,a.status,a.solution,a.record_time,a.create_time
                 FROM
                    zay_abnormal as a
                 LEFT JOIN zay_barn as b ON a.abnormal_type = " . AbnormalType::TYPE_BARN . " AND a.case_id = b.out_id
                 LEFT JOIN zay_fence as fe ON a.abnormal_type = " . AbnormalType::TYPE_FENCE . " AND a.case_id = fe.out_id
                 LEFT JOIN zay_pig as p ON a.abnormal_type = " . AbnormalType::TYPE_PIG . " AND a.case_id = p.out_id
                 LEFT JOIN zay_feed as feed ON a.abnormal_type = " . AbnormalType::TYPE_FEED . " AND a.case_id = feed.out_id
                 LEFT JOIN zay_medicine as medicine ON a.abnormal_type = " . AbnormalType::TYPE_MEDICINE . " AND a.case_id = medicine.out_id
                 LEFT JOIN zay_machine as m ON a.abnormal_type = " . AbnormalType::TYPE_DEVICE . " AND a.case_id = m.id
                 LEFT JOIN zay_factory as f ON a.factory_id = f.id
                 LEFT JOIN zay_barn as b1 ON a.barn_id = b1.out_id
                 LEFT JOIN zay_fence as fe1 ON a.fence_id = fe1.id
                 LEFT JOIN zay_abnormal_type as sat ON a.sec_abnormal_type_id = sat.out_id 
                 WHERE 
                    a.factory_id = :factory_id 
                 AND 
                    a.delete_time = :delete_time
        ";
        $totalSql = "SELECT 
                        COUNT(a.id) as count
                     FROM
                        zay_abnormal as a
                     LEFT JOIN zay_barn as b ON a.abnormal_type = " . AbnormalType::TYPE_BARN . " AND a.case_id = b.out_id
                     LEFT JOIN zay_fence as fe ON a.abnormal_type = " . AbnormalType::TYPE_FENCE . " AND a.case_id = fe.out_id
                     LEFT JOIN zay_pig as p ON a.abnormal_type = " . AbnormalType::TYPE_PIG . " AND a.case_id = p.out_id
                     LEFT JOIN zay_feed as feed ON a.abnormal_type = " . AbnormalType::TYPE_FEED . " AND a.case_id = feed.out_id
                     LEFT JOIN zay_medicine as medicine ON a.abnormal_type = " . AbnormalType::TYPE_MEDICINE . " AND a.case_id = medicine.out_id
                     LEFT JOIN zay_machine as m ON a.abnormal_type = " . AbnormalType::TYPE_DEVICE . " AND a.case_id = m.id
                     LEFT JOIN zay_factory as f ON a.factory_id = f.id
                     LEFT JOIN zay_barn as b1 ON a.barn_id = b1.out_id
                     LEFT JOIN zay_fence as fe1 ON a.fence_id = fe1.id
                     LEFT JOIN zay_abnormal_type as sat ON a.sec_abnormal_type_id = sat.out_id 
                 WHERE 
                    a.factory_id = :factory_id
                 AND 
                    a.delete_time = :delete_time 
        ";
        $where = [
            'factory_id' => $this->factory_id,
            'delete_time' => $this->model::IS_DELETE_NO
        ];
        if ($param['barn_id']) {
            $sql .= " AND a.barn_id = :barn_id";
            $totalSql .= " AND a.barn_id = :barn_id";
            $where['barn_id'] = $param['barn_id'];
        }
        if ($param['fence_id']) {
            $sql .= " AND a.fence_id = :fence_id ";
            $totalSql .= " WHERE a.fence_id = :fence_id ";
            $where['fence_id'] = $param['fence_id'];
        }
        if ($param['case_type']) {
            if (!in_array($param['case_type'], [AbnormalType::TYPE_BARN, AbnormalType::TYPE_FENCE, AbnormalType::TYPE_PIG, AbnormalType::TYPE_FEED, AbnormalType::TYPE_MEDICINE, AbnormalType::TYPE_DEVICE])) {
                $sql .= " AND a.abnormal_type = :case_type ";
                $totalSql .= " AND a.abnormal_type = :case_type ";
            } else {
                $sql .= " AND a.sec_abnormal_type_id = :case_type ";
                $totalSql .= " AND a.sec_abnormal_type_id = :case_type ";
            }
            $where['case_type'] = $param['case_type'];
        }
        if ($param['record_time_start']) {
            $sql .= " AND a.record_time >= :record_time_start ";
            $totalSql .= " AND a.record_time >= :record_time_start ";
            $where['record_time_start'] = strtotime($param['record_time_start']);
        }
        if ($param['record_time_end']) {
            $sql .= " AND a.record_time <= :record_time_end ";
            $totalSql .= " AND a.record_time <= :record_time_end ";
            $where['record_time_end'] = strtotime($param['record_time_end']);
        }

        $total = Db::query($totalSql, $where);

        $offset = ($param['page'] - 1) * $param['page_size'];
        $sql .= " ORDER BY a.out_id DESC LIMIT " . $offset . "," . $param['page_size'];
        $data = Db::query($sql, $where);

        return [
            "total" => $total[0]['count'],
            "per_page" => $param['page_size'],
            "current" => $param['page'],
            "last_page" => ceil($total[0]['count'] / $param['page_size']),
            "data" => $data
        ];
    }

}